#  ** *********************************************************************

#  ************************************************************************

# /bin/bash
hive_db="hive_dwd"
hive_table="order_base"
doris_db="hive_dwd"
doris_table="order_base"

#获取考核月份，每月5号前只统计上一月数据，5号后统计下一月数据
stats_day=$(date "+%d")
if [ "$stats_day" = "01" ] || [ "$stats_day" = "02" ] || [ "$stats_day" = "03" ] || [ "$stats_day" = "04" ]; then
  stats_month=$(date -d"-1 month" +%y%m)
  partition_month=$(date "+%y%m")
  min_date=$(date -d"-2 month" +%Y-%m-%d)
  max_date=$(date -d"+1 month" +%Y-%m-%d)
else
  stats_month=$(date "+%y%m")
  partition_month=$(date -d"+1 month" +%y%m)
  min_date=$(date -d"-1 month" "+%Y-%m-01")
  max_date=$(date -d"+2 month" +%Y-%m-%d)
fi

#获取配置中的dorisdb信息
source /app/module/bigdata-schedule/conf/config.sh
doris_cmd="mysql -h${doris_db_vts_ip} -P ${doris_db_vts_port} -u ${doris_db_vts_user} -B -N -e "
if [ "$doris_db_vts_pwd" != "" ]; then
  doris_cmd="mysql -h${doris_db_vts_ip} -P ${doris_db_vts_port} -u ${doris_db_vts_user} -p${doris_db_vts_pwd} -B -N -e "
fi

#重建临时分区
$doris_cmd "ALTER TABLE ${doris_db}.${doris_table} drop temporary partition if exists tp$stats_month force;"
$doris_cmd "ALTER TABLE ${doris_db}.${doris_table} add temporary partition if not exists tp$stats_month values [('$stats_month'),('$partition_month'));"

#建分区
$doris_cmd "ALTER TABLE ${doris_db}.${doris_table} add partition if not exists p$stats_month values [('$stats_month'),('$partition_month'));"

#导入分区数据
label_id="${doris_table}_$(date +%s)"
load_sql="
use $doris_db;
load label ${label_id}
(
    data infile('hdfs://bigWorld/user/hive/warehouse/${hive_db}.db/${hive_table}/*')
    into table ${doris_table}
    TEMPORARY PARTITION (tp${stats_month})
    FORMAT AS 'ORC'
    (
          id
        , code
        , order_id
        , distance
    )
    SET(stats_month=${stats_month})
    WHERE car_type is not null and loading_date >= '${min_date} 00:00:00' and loading_date <= '${max_date} 23:59:59'
)
with broker 'broker1'
PROPERTIES
(
    'max_filter_ratio' = '1',
    'strict_mode' = 'false'
);
"

#提交
$doris_cmd "$load_sql"
[ $? -ne 0 ] && exit 1

#轮训查询导入状态 默认200分钟超时脚本返回失败，可根据数据量大小适当调整
for i in $(seq 200); do
  state=$($doris_cmd "use ${doris_db};show load where label = '$label_id'" | awk '{print $3}' | tail -1)
  if [ "$state" = "FINISHED" ]; then
    echo "导入成功"
    break
  elif [ "$state" = "CANCELLED" ]; then
    echo $($doris_cmd "use ${doris_db};show load where label = '$label_id'")
    echo "导入失败"
    exit 1
  elif [ "$state" = "" ]; then
    echo "参数有误"
    exit 1
  else
    echo "导入中"
    sleep 10s
    continue
  fi
  echo "导入超时"
  exit 1
done

#替换临时分区
$doris_cmd "alter table ${doris_db}.${doris_table} replace partition (p${stats_month}) with temporary partition (tp${stats_month}) properties ('strict_range' = 'false','use_temp_partition_name'='false');"
